package com.link2room.rodimus.util;

import com.link2room.common.util.DateUtil;
import org.apache.commons.collections.CollectionUtils;

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

/**
 * Created by LN on 2018/1/3
 */
public final class CalendarUtils {
    /**
     * 获得 当前 年月日
     *
     * @return
     */
    public static final Date getDateWithOutTime(LocalDate localDate) {

        if (localDate == null) {
            throw new NullPointerException("calendar can not be null.");
        }
        Calendar c = Calendar.getInstance();
        c.set(localDate.getYear(), localDate.getMonthValue() - 1, localDate.getDayOfMonth(), 0, 0, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

    /**
     * 获得 当前 年月日
     *
     * @return
     */
    public static final Date getDateWithOutTime(Date date) {

        if (date == null) {
            throw new NullPointerException("calendar can not be null.");
        }
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDate localDate = instant.atZone(zoneId).toLocalDate();
        Calendar c = Calendar.getInstance();
        c.set(localDate.getYear(), localDate.getMonthValue() - 1, localDate.getDayOfMonth(), 0, 0, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }


    /**
     * 获得当前月份开始时间
     *
     * @return
     */
    public static final Date getStartTimeOfMonth(Date date) {
        Calendar.getInstance(Locale.CHINA);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();

    }


    /**
     * 获得当前月份结束时间
     *
     * @return
     */
    public static final Date getEndTimeOfMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.MILLISECOND, 999);
        return cal.getTime();
    }

    /**
     * 返回 dates 中连续dayStep天数个数
     *
     * @param dates
     * @param dayStep 连续天数
     * @return
     */
    public static int countSeries(List<Date> dates, int dayStep) {
        if (CollectionUtils.isEmpty(dates)) {
            throw new NullPointerException("dates is required.");
        }
        if (dayStep < 1) {
            throw new IllegalArgumentException("dayStep must be greater than 1.");
        }
        int count = 0;
        dates = dates.stream().distinct().collect(Collectors.toList());
        dates.sort(Date::compareTo);
        for (int i = 0; i < dates.size(); ) {
            Date date = dates.get(i);
            for (int j = 0; j < dayStep; j++) {
                if (!dates.contains(DateUtil.addDays(date, j))) {
                    i = i + j ;
                    break;
                }else {
                    if (j == dayStep-1) {
                        count++;
                        i = i + j + 1;
                    }
                }
            }
        }
        return count;
    }

}
